import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'
// import routers from './route'
import { Message } from 'element-ui'
/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const constantRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },

  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    children: [{
      path: 'dashboard',
      name: 'Dashboard',
      component: () => import('@/views/dashboard/index'),
      meta: { title: '控制面板', icon: 'dashboard' }
    }]
  },
  {
    path: '/goods',
    component: Layout,
    redirect: '/goods/index',
    name: 'goods',
    meta: { title: '商品管理', icon: 'el-icon-shopping-bag-1' },
    children: [
      {
        path: 'index',
        name: 'goodsIndex',
        component: () => import('@/views/goods/index'),
        meta: { title: '上架中', icon: 'el-icon-goods' }
      },
      {
        path: 'stock',
        name: 'stock',
        component: () => import('@/views/goods/offsale'),
        meta: { title: '仓库中', icon: 'el-icon-shopping-bag-1' }
      },
      {
        path: 'delete',
        name: 'delete',
        component: () => import('@/views/goods/delete'),
        meta: { title: '回收站', icon: 'el-icon-delete\n' }
      },
      {
        path: 'form',
        name: 'goodsForm',
        hidden: true,
        component: () => import('@/views/goods/form'),
        meta: { title: '操作表单', icon: 'form' }
      },
      {
        path: 'goodsCategory',
        name: 'goodsCategory',
        component: () => import('@/views/goods/category/index'),
        meta: { title: '分类管理', icon: 'category' }
      },
      {
        path: 'goodsCategoryForm',
        name: 'goodsCategoryForm',
        hidden: true,
        component: () => import('@/views/goods/category/form'),
        meta: { title: '操作表单', icon: 'form' }
      },
      {
        path: 'goodsBrand',
        name: 'goodsBrand',
        component: () => import('@/views/goods/brand/index'),
        meta: { title: '品牌管理', icon: 'category' }
      },
      {
        path: 'goodsBrandForm',
        name: 'goodsBrandForm',
        hidden: true,
        component: () => import('@/views/goods/brand/form'),
        meta: { title: '操作表单', icon: 'form' }
      }
    ]
  },
  {
    path: '/order',
    component: Layout,
    redirect: '/order/index',
    name: 'order',
    meta: { title: '订单管理', icon: 'el-icon-document-copy' },
    children: [
      {
        path: 'index',
        name: 'index',
        component: () => import('@/views/order/index'),
        meta: { title: '订单列表', icon: 'el-icon-document-copy' }
      },
      {
        path: 'need_send',
        name: 'need_send',
        component: () => import('@/views/order/need_send'),
        meta: { title: '待发货', icon: 'need-send' }
      },
      {
        path: 'need_receive',
        name: 'need_receive',
        component: () => import('@/views/order/need_receive'),
        meta: { title: '待收货', icon: 'need-send' }
      },
      {
        path: 'need_check',
        name: 'need_check',
        component: () => import('@/views/order/need_check'),
        meta: { title: '待审核', icon: 'check' }
      },
      {
        path: 'finished',
        name: 'need_receive',
        component: () => import('@/views/order/finished'),
        meta: { title: '已完成', icon: 'finished' }
      },
      {
        path: 'refund',
        name: 'refund',
        component: () => import('@/views/order/refund'),
        meta: { title: '售后中', icon: 'refund' }
      },
      {
        path: 'detail',
        name: 'orderDetail',
        hidden: true,
        component: () => import('@/views/order/detail'),
        meta: { title: '订单详情', icon: 'need-pay' }
      }
    ]
  },
  {
    path: '/marketing',
    component: Layout,
    redirect: '/marketing/index',
    name: 'marketing',
    meta: { title: '营销管理', icon: 'marketing' },
    children: [
      {
        path: 'index',
        name: 'couponIndex',
        component: () => import('@/views/coupon/index'),
        meta: { title: '优惠券列表', icon: 'coupon' }
      },
      {
        path: 'form',
        name: 'couponForm',
        hidden: true,
        component: () => import('@/views/coupon/form'),
        meta: { title: '操作表单', icon: 'form' }
      },
      {
        path: 'coupon_log',
        name: 'couponLog',
        component: () => import('@/views/coupon/coupon_log'),
        meta: { title: '优惠券记录', icon: 'el-icon-document' }
      },
      {
        path: 'rechargePackage',
        name: 'rechargePackage',
        component: () => import('@/views/setting/recharge_package'),
        meta: { title: '充值套餐配置', icon: 'el-icon-money' }
      },
      {
        path: 'rechargePackageForm',
        name: 'rechargePackageForm',
        hidden: true,
        component: () => import('@/views/setting/recharge_package_form'),
        meta: { title: '充值套餐配置表单', icon: 'el-icon-form' }

      }
    ]
  },
  {
    path: '/shop',
    component: Layout,
    redirect: 'navsIndex',
    name: 'shop',
    meta: { title: '商城配置', icon: 'shop' },
    children: [
      {
        path: 'navsIndex',
        name: 'navsIndex',
        component: () => import('@/views/navs/index'),
        meta: { title: '导航列表', icon: 'navs' }
      },
      {
        path: 'navsForm',
        name: 'navsForm',
        hidden: true,
        component: () => import('@/views/navs/form'),
        meta: { title: '操作表单', icon: 'form' }
      },
      {
        path: 'bannerIndex',
        name: 'bannerIndex',
        component: () => import('@/views/banner/index'),
        meta: { title: '轮播列表', icon: 'el-icon-picture-outline' }
      },
      {
        path: 'bannerForm',
        name: 'bannerForm',
        hidden: true,
        component: () => import('@/views/banner/form'),
        meta: { title: '操作表单', icon: 'form' }
      },
      {
        path: 'payment',
        name: 'payment',
        component: () => import('@/views/setting/payment.vue'),
        meta: { title: '支付配置', icon: 'el-icon-money' }
      }
    ]
  },

  {
    path: '/user',
    component: Layout,
    redirect: '/user/index',
    name: 'user',
    meta: { title: '用户管理', icon: 'el-icon-user' },
    children: [
      {
        path: 'index',
        name: 'userIndex',
        component: () => import('@/views/user/index'),
        meta: { title: '用户列表', icon: 'user' }
      },
      {
        path: 'form',
        name: 'userForm',
        hidden: true,
        component: () => import('@/views/user/form'),
        meta: { title: '操作表单', icon: 'form' }
      },
      {
        path: 'userRecharge',
        name: 'userRecharge',
        hidden: true,
        component: () => import('@/views/user/recharge'),
        meta: { title: '充值', icon: 'el-icon-star-on' }
      },
    ]
  },

  {
    path: '/setting',
    component: Layout,
    redirect: '/setting',
    name: 'log',
    meta: { title: '设置中心', icon: 'el-icon-setting' },
    children: [
      {
        path: 'edit_pwd',
        name: 'edit_pwd',
        component: () => import('@/views/setting/edit_pwd.vue'),
        meta: { title: '修改密码', icon: 'el-icon-bank-card' }
      },
      {
        path: 'index',
        name: 'index',
        component: () => import('@/views/setting/index.vue'),
        meta: { title: '配置管理', icon: 'el-icon-cpu' }
      },
      {
        path: 'form',
        name: 'form',
        hidden: true,
        component: () => import('@/views/setting/form.vue'),
        meta: { title: '操作表单', icon: 'el-icon-cpu' }
      },
      {
        path: 'upload',
        name: 'upload',
        component: () => import('@/views/setting/upload.vue'),
        meta: { title: '上传配置', icon: 'el-icon-upload2' }
      }

    ]
  },
  {
    path: '/system',
    component: Layout,
    redirect: '/system',
    name: 'log',
    meta: { title: '系统日志', icon: 'el-icon-tickets' },
    children: [
      {
        path: 'queue',
        name: 'queue',
        component: () => import('@/views/queue/index.vue'),
        meta: { title: '队列管理', icon: 'el-icon-time' }
      },
      {
        path: 'queue_form',
        name: 'queue_form',
        hidden: true,
        component: () => import('@/views/queue/form.vue'),
        meta: { title: '操作表单', icon: 'el-icon-cpu' }
      },
      {
        path: 'access_log',
        name: 'access_log',
        component: () => import('@/views/access_log/index.vue'),
        meta: { title: '请求记录', icon: 'el-icon-c-scale-to-original' }
      },
      {
        path: 'access_log_form',
        name: 'access_log_form',
        hidden: true,
        component: () => import('@/views/access_log/form.vue'),
        meta: { title: '操作表单', icon: 'el-icon-cpu' }
      },
      {
        path: 'upload_log',
        name: 'upload_log',
        component: () => import('@/views/upload_log/index.vue'),
        meta: { title: '上传记录', icon: 'el-icon-folder-opened' }
      },
      {
        path: 'upload_log_form',
        name: 'upload_log_form',
        hidden: true,
        component: () => import('@/views/upload_log/form.vue'),
        meta: { title: '操作表单', icon: 'el-icon-cpu' }
      },
      {
        path: 'code_log',
        name: 'code_log',
        component: () => import('@/views/code_log/index.vue'),
        meta: { title: '验证码记录', icon: 'el-icon-chat-dot-square' }
      },
      {
        path: 'sys_statistics',
        name: 'sys_statistics',
        component: () => import('@/views/sys_statistics/index.vue'),
        meta: { title: '数据统计', icon: 'el-icon-data-line' }
      },
      {
        path: 'creditLog',
        name: 'creditLog',
        component: () => import('@/views/log/credit/index.vue'),
        meta: { title: '余额记录', icon: 'el-icon-money' }
      },
      {
        path: 'rechargeLog',
        name: 'rechargeLog',
        component: () => import('@/views/log/rechange/index.vue'),
        meta: { title: '充值记录', icon: 'el-icon-money' }
      },
      {
        path: 'PointLog',
        name: 'PointLog',
        component: () => import('@/views/log/credit/point.vue'),
        meta: { title: '积分记录', icon: 'el-icon-money' }
      }
    ]
  },
  {
    path: '/package/index',
    component: Layout,
    redirect: '/package/index',
    name: '工具中心',
    meta: { title: '工具中心', icon: 'el-icon-cpu' },
    children: [
      {
        path: '/package/index',
        name: 'packageIndex',
        component: () => import('@/views/package/index'),
        meta: { title: '更新管理', icon: 'package' }
      },
      {
        path: '/package/form',
        name: 'packageForm',
        hidden: true,
        component: () => import('@/views/package/form'),
        meta: { title: '操作表单', icon: 'form' }
      },
      {
        path: 'notice/index',
        name: 'noticeIndex',
        component: () => import('@/views/notice/index'),
        meta: { title: '通知列表', icon: 'el-icon-bell' }
      },
      {
        path: 'notice/form',
        name: 'noticeForm',
        hidden: true,
        component: () => import('@/views/notice/form'),
        meta: { title: '操作表单', icon: 'form' }
      },
      {
        path: 'update_log/index',
        name: 'index',
        component: () => import('@/views/update_log/index'),
        meta: { title: '更新记录', icon: 'el-icon-refresh\n' }
      },
      {
        path: 'update_log/form',
        name: 'updateLogForm',
        hidden: true,
        component: () => import('@/views/update_log/form'),
        meta: { title: '操作表单', icon: 'form' }
      },
      {
        path: 'plugins/index',
        name: 'plugins/index',
        component: () => import('@/views/plugin/index'),
        meta: { title: '插件管理', icon: 'el-icon-set-up' }
      },
      {
        path: 'plugins/form',
        name: 'plugins/form',
        hidden: true,
        component: () => import('@/views/plugin/form'),
        meta: { title: '操作表单', icon: 'form' }
      },
      {
        path: 'help/index',
        name: 'helpIndex',
        component: () => import('@/views/help/index'),
        meta: { title: '帮助管理', icon: 'help' }
      },
      {
        path: 'help/form',
        name: 'helpForm',
        hidden: true,
        component: () => import('@/views/help/form'),
        meta: { title: '操作表单', icon: 'form' }
      },
      {
        path: 'curdForm',
        name: 'curdForm',
        component: () => import('@/views/curd/form'),
        meta: { title: '一键CURD', icon: 'el-icon-cpu' }
      },
      {
        path: 'curdPreview',
        name: 'curdPreview',
        hidden: true,
        component: () => import('@/views/curd/preview'),
        meta: { title: '一键CURD', icon: 'el-icon-cpu' }
      }
    ]
  },
  {
    path: '/kefu',
    component: Layout,
    redirect: '/kefu/index',
    name: '客服中心',
    meta: { title: '客服中心', icon: 'el-icon-headset' },
    children: [
      {
        path: 'kefuIndex',
        name: 'kefuIndex',
        component: () => import('@/views/kefu/conversations'),
        meta: { title: '会话管理', icon: 'el-icon-headset' }
      },
      {
        path: 'kefuChat',
        name: 'kefuChat',
        hidden: true,
        component: () => import('@/views/kefu/chat'),
        meta: { title: '聊天窗口', icon: 'el-icon-headset' }
      },
      {
        path: 'kefuList',
        name: 'kefuList',
        hidden: false,
        component: () => import('@/views/kefu/kefu_list'),
        meta: { title: '客服管理', icon: 'el-icon-user' }
      },
      {
        path: 'kefuForm',
        name: 'kefuForm',
        hidden: true,
        component: () => import('@/views/kefu/kefu_form'),
        meta: { title: '操作表单', icon: 'form' }
      },
      {
        path: '/feedback/index',
        name: 'feedbackIndex',
        component: () => import('@/views/feedback/index'),
        meta: { title: '留言反馈', icon: 'feedback' }
      }
    ]
  },
  {
    path: '/distribution',
    component: Layout,
    redirect: '/distribution/index',
    name: '分销管理',
    meta: { title: '分销管理', icon: 'el-icon-user\n' },
    children: [
      {
        path: 'distributionIndex',
        name: '分销订单',
        component: () => import('@/views/distribution/order'),
        meta: { title: '分销订单', icon: 'el-icon-document-copy' }
      },
      {
        path: 'distributionUser',
        name: 'distributionUser',
        hidden: false,
        component: () => import('@/views/distribution/users'),
        meta: { title: '分销员', icon: 'el-icon-user\n' }
      },
      {
        path: 'distributionUserForm',
        name: 'distributionUserForm',
        hidden: true,
        component: () => import('@/views/distribution/userForm'),
        meta: { title: '人员表单', icon: 'form' }
      }
    ]
  },
  {
    path: '/admin',
    component: Layout,
    redirect: '/admin/index',
    name: '管理和权限',
    meta: { title: '管理和权限', icon: 'el-icon-user\n' },
    children: [
      {
        path: 'adminIndex',
        name: 'adminIndex',
        component: () => import('@/views/admin/index'),
        meta: { title: '管理员', icon: 'el-icon-user\n' }
      },
      {
        path: 'adminForm',
        name: 'adminForm',
        hidden: true,
        component: () => import('@/views/admin/form'),
        meta: { title: '操作表单', icon: 'form'}
      },
      {
        path: 'roleList',
        name: 'roleList',
        component: () => import('@/views/admin/roleList'),
        meta: { title: '角色管理', icon: 'el-icon-user\n' }
      },
      {
        path: 'roleForm',
        name: 'roleForm',
        hidden: true,
        component: () => import('@/views/admin/roleForm'),
        meta: { title: '操作表单', icon: 'form' }
      },
      {
        path: 'roleAuths',
        name: 'roleAuths',
        hidden: true,
        component: () => import('@/views/admin/roleAuths'),
        meta: { title: '权限列表', icon: ''}
      },
      {
        path: 'menuList',
        name: 'menuList',
        component: () => import('@/views/admin/menuList'),
        meta: { title: '菜单列表', icon: 'el-icon-user\n' }
      },
      {
        path: 'menuForm',
        name: 'menuForm',
        hidden: true,
        component: () => import('@/views/admin/menuForm'),
        meta: { title: '操作表单', icon: 'form' }
      }
    ]
  },
  {
    path: '/admin_msg',
    component: Layout,
    redirect: '/admin_msg',
    name: '消息管理',
    meta: { title: '消息管理', icon: 'el-icon-msg' },
    children: [
      {
        path: 'msgList',
        name: 'msgList',
        component: () => import('@/views/admin_msg/list'),
        meta: { title: '消息列表', icon: 'el-icon-msg' }
      }
    ]
  }

  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]
//console.log(routers)
const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRoutes
})

const router = createRouter()
router.beforeEach((to, from, next) => {
  // 在路由切换前触发的钩子函数
  // to: 要进入的路由对象
  // from: 当前导航正要离开的路由对象
  // next: 必须调用该方法来resolve钩子，即执行导航操作

  // 检查目标路由是否存在
  if (to.matched.length <= 0) {
    console.error('路由不存在:' + to.path)
    Message.error('路由不存在:' + to.path)
    return false
  } else {
    next() // 必须调用next()来进行路由导航
  }
})

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router


